package com.example.myworkers.controller;


import com.example.myworkers.model.User;
import com.example.myworkers.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "user")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "查询所有用户信息",notes = "获取用户信息返回json字符串")
    @RequestMapping("/getUser")
    public String getUser(){
        return "Hello,User";
    }

    @CrossOrigin
    @ApiOperation(value = "对用户的添加方法")
    @GetMapping("/addUser")
    public int addUSer(User user){
        System.out.println("进入添加用户信息接口");
        return userService.insertUser(user);
    }

    @ApiOperation(value = "对用户信息的全部查询方法")
    @GetMapping("/getAll")
    public List<User> getAllUser(User user){
        System.out.println("进入查询方法");
        List<User> listUser=userService.getAllUser(user);
        return listUser;
    }

    @ApiOperation(value = "对用户信息的修改方法")
    @PostMapping("/editUser")
    public int EditUser(@RequestBody User user){
        System.out.println("进入修改用户信息方法");
        return userService.updateUser(user);
    }

    @ApiOperation(value = "对用户信息的删除方法")
    @GetMapping("/delUser")
    public int Delete(int user_id){
        System.out.println("进入删除方法");
        int returnValue=0;
        if(user_id!=0){
            User user=new User();
            user.setUser_id(user_id);
            returnValue=userService.deleteUser(user);
        }
        return returnValue;
    }

    @ApiOperation(value = "对用户信息的单个查询方法")
    @GetMapping("/getOne")
    public User getUser(int user_id){
        User user=new User();
        System.out.println("进入单条查询方法");
        if(user_id!=0){
            User user1=new User();
            user1.setUser_id(user_id);
            user=userService.getUser(user1);
            System.out.println("返回的数据集合为："+user.getUser_name());
        }
        return user;
    }
}
